<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    class App extends React.Component{
        state={
            msg:"奔跑吧！少年！！！！",
            // 保存计时器 ，若为null则没有任何计时任务 若不为null则当前存在计时任务
            timer:null
        }
        render(){
            let {msg} = this.state;
            return (
                <div>
                    <h1>{msg}</h1>  
                    <button onClick={()=>this.start()}>开始</button>
                    <button onClick={()=>this.pause()}>暂停</button>  
                </div>
            )
        }
        start(){
        //判断当前是否有计时任务
        if(this.state.timer){
            return
        }
        this.state.timer = setInterval(()=>{
            let {msg} = this.state;
            // 截取第一个字符
            let str = msg.substr(0,1);
            // 截取剩余的字符
            let str1 = msg.substr(1)
            console.log(str1+str)
            // 拼接成新字符
            let newMsg = str1+str;
            this.setState({
                msg:newMsg
            })
           },50)

           // 更新状态
        this.setState({
            timer:this.state.timer
        })
        }
        // 暂停 
        pause(){
            clearInterval(this.state.timer)
            this.setState({
                timer:null
            })
        }
    }


    ReactDOM.render(<App/>,document.getElementById("root"))
    
</script>
</html>